<?php

/*
Process Add Enemy (admin_add_enemy.pro.php)
*/

$rank_check = "7";
include "../global.inc.php";

$game=$_GET['game'];
$add_pet_name = $_POST['add_pet_name'];
$add_level = $_POST['add_level'];
$to_kill = $_POST['to_kill'];
$add_strength = $_POST['add_strength'];
$add_defense = $_POST['add_defense'];
$add_speed = $_POST['add_speed'];
$add_health = $_POST['add_health'];
$attack_pattern = $_POST['attack_pattern'];

$explodeAttacks = explode(", ", $attack_pattern);
$countAttacks = count($explodeAttacks) - 1;

if ($countAttacks != "0")
{
	for ($a = 0; $a <= $countAttacks;)
	{
		$findItem = fetch("SELECT * FROM attacks2 WHERE attack_name = '$explodeAttacks[$a]' AND game = '$game'");
		if (!$findItem[id])
		{
			die(header(error("admin_add_enemy.php?game=$game","The attack \"$explodeAttacks[$a]\" does not exsist!")));
		}
		$a++;
	}
}

$directoryName = "../images/user_images/opg_$game";
if (!file_exists($directoryName)) { mkdir($directoryName, 0777); }

$directoryName2 = "../images/user_images/opg_$game/enemies";
if (!file_exists($directoryName2)) { mkdir($directoryName2, 0777); }

if ($HTTP_POST_FILES['petImage']['size'] >= 32000)
{
	die(header(error("admin_add_enemy.php?game=$game", "Your image must be under 32,000 bytes (or 32kb)")));
}

if (!eregi("image/", $HTTP_POST_FILES['petImage']['type']))
{
	die(header(error("admin_add_enemy.php?game=$game", "You should only use .gif, .jpg, and .jpeg files for images. Please convert the image to one of these file types.")));
}

if (!$add_pet_name)
{
	die(header(error("admin_add_enemy.php?game=$game", "Please do not leave any information blank!")));
}

$check = fetch("SELECT id FROM one_players2 WHERE name = '$add_pet_name' AND game = '$game'");

if ($check[id])
{
	die(header(error("admin_add_enemy.php?game=$game", "There is already an enemy with the name $add_pet_name.")));
}
else
{
	mysql_query("INSERT INTO one_players2 (name,level,guys_left,strength,defense,speed,health,attack_pattern,game) VALUES ('$add_pet_name','$add_level','$to_kill','$add_strength','$add_defense','$add_speed','$add_health','$attack_pattern','$game')");
	$insert_id = mysql_insert_id();
	
	$file = $HTTP_POST_FILES['petImage']['tmp_name'];
	$dest = "../images/user_images/opg_$game/enemies/enemy_$insert_id.gif";
	
	copy($file, $dest);
	
	header("Location: admin_add_enemy.php?game=$game&error=The+pet+$add_pet_name+has+been+added!");
}

?>
